package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import 学生.*;
import 数据库和网络.*;

public class teacherWindow extends JFrame implements Runnable{
    JPanel mainPanel;

    public teacherWindow(){}

    public teacherWindow(String a){
        mainPanel = new JPanel();

        this.setSize(500,460);
        mainPanel.setSize(this.getSize());
        this.setContentPane(mainPanel);
        this.setLocationRelativeTo(null);//居中
        this.setTitle("在线考试平台——教师端");
        this.setVisible(true);
        // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Runnable s=new Runnable(){
            @Override
            public void run(){
                String name="";
                String ID="";
                try {
                    myDatebase nameGetter=new myDatebase("teacher");
                    String sql="select * from teacher.teacher where teacherID='"+enterWindow.IDText.getText()+"'";
                    nameGetter.statement.execute(sql);
                    nameGetter.resultSet=nameGetter.statement.executeQuery(sql);
                    while(nameGetter.resultSet.next()){
                        name=nameGetter.resultSet.getString(1);
                        enterWindow.currentTeacher.setName(name);
                        ID=nameGetter.resultSet.getString(2);
                        enterWindow.currentTeacher.setSchoolID(ID);}
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                JLabel infoLabel = new JLabel("ID:"+ID+"   "+name+"老师，你好！");
                infoLabel.setBounds(20,50,300,40);
                JButton testButton = new JButton("查看、编辑试题");
                JButton rankButton = new JButton("查看成绩");
                JButton reviseButton=new JButton("批改");
                JLabel label2 = new JLabel();
                JButton backButton = new JButton("退出登录");
                mainPanel.setLayout(null);

                testButton.setBounds(160,100,180,50);
                mainPanel.add(testButton);
                rankButton.setBounds(160,170,180,50);
                mainPanel.add(rankButton);
                mainPanel.add(label2);
                backButton.setBounds(305,330,110,40);
                mainPanel.add(backButton);
                reviseButton.setBounds(160,240,180,50);
                mainPanel.add(reviseButton);
                testButton.setBackground(Color.lightGray);
                rankButton.setBackground(Color.lightGray);
                backButton.setBackground(Color.lightGray);
                reviseButton.setBackground(Color.lightGray);
                testButton.addActionListener(new testListener());
                rankButton.addActionListener(new rankChooseListener());
                backButton.addActionListener(new backListener());
                reviseButton.addActionListener(new reviseChooseListener());
                mainPanel.add(infoLabel);
                mainPanel.add(testButton);
                mainPanel.add(rankButton);
                mainPanel.add(backButton);
                infoLabel.setHorizontalAlignment(SwingConstants.CENTER);
                infoLabel.setVerticalAlignment(SwingConstants.CENTER);

                enterWindow.currentTeacher.setName(name);
                enterWindow.currentTeacher.setSchoolID(ID);

            }
        };
        Thread thread2=new Thread(s);
        thread2.run();

    }

    public class testListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            teacherWindow.this.dispose();
            new questionWindow();
        }
    }



    public class backListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {//关闭注册界面
            teacherWindow.this.dispose();
            new enterWindow();
        }
    }

    public class reviseChooseListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){

            teacherWindow.this.dispose();
            chooseStudentWindow.chooseStudent =new chooseStudentWindow();
        }
    }

    public class rankChooseListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){

            teacherWindow.this.dispose();
            chooseRankStudentWindow.chooseRank =new chooseRankStudentWindow();
        }
    }
    @Override
    public void run(){
        new studentWindow("a");
    }
}
